package JdbcUitils;
//配置原生的jdbc

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.SynchronousQueue;

import JdbcUitils.FromGpsToBaidu;
import JdbcUitils.StringUtil;

public class InsertCyclingFromImeiDao {
	Connection con = null;
	PreparedStatement ps = null;
	Statement stmt = null;
	ResultSet rs = null;
	public Connection getConnection(){
//		try {
//			Class.forName("com.mysql.jdbc.Driver");// 驱动
//		}catch (ClassNotFoundException ex) {
//			System.out.println("驱动没有加载");
//		}
//		try {
//			con = DriverManager.getConnection(dburl, user, password);
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		return DataSourceCon.getConnectionFromc3p0();
	}
	
	
	//向IMEIinfo表中插入数据
	public void InsertIntoIMEIinfoDao(String sql1,String[] infoSplit){
		
		con = getConnection();
		try {
			//stmt = con.createStatement();
			con.setAutoCommit(false);
			//String sql1 = "INSERT INTO imei_info VALUES(replace(uuid(),'-',''),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			ps = con.prepareStatement(sql1);
			ps.setString(1, infoSplit[0]);
			ps.setString(2, infoSplit[1]);
			ps.setString(3, infoSplit[2]);
	        
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
			ps.setString(4, sdf.format(new Date()));
			
			
			ps.setString(5, infoSplit[5]+","+infoSplit[7]);
			ps.setString(6, infoSplit[5]);
			ps.setString(7, infoSplit[7]);
			ps.setString(8, infoSplit[8]);
			ps.setString(9, infoSplit[9]);
			ps.setString(10, infoSplit[10]);
			ps.setString(11, infoSplit[11]);
			ps.setString(12, infoSplit[12]);
			ps.setString(13, infoSplit[13]);
			if(infoSplit.length<10){
				ps.setString(14, "0.0");
			}else{
				ps.setString(14, "1.0");
			}
			if(!StringUtil.isEmpty(infoSplit[7])&&!StringUtil.isEmpty(infoSplit[5])){
				String address = new FromGpsToBaidu().getaddressBylnglat(infoSplit[7]+","+infoSplit[5]);
				ps.setString(15, address);
			}else{
				ps.setString(15, "无数据");
			}
			ps.setString(16, infoSplit[infoSplit.length-1]);
			ps.setString(17, infoSplit[3]);//硬件字符串
			
			ps.executeUpdate();
			con.commit();
			System.out.println("已增至imeiinfo表");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		//closeall();
	}
	
	
	
	
	
	public void closeall(){
		if (rs != null) {  
	        try {  
	            rs.close();  
	        } catch (SQLException e) { /* ignored */}  
	    }  
	    if (ps != null) {  
	        try {  
	            ps.close();  
	        } catch (SQLException e) { /* ignored */}  
	    }  
	    if (con != null) {  
	        try {  
	            con.close();  
	        } catch (SQLException e) { /* ignored */}  
	    }
	}


	//根据一个字段值获取另一个字段值
	public String getStrByColumnDao(String sql, String[] psarr) {
		// TODO Auto-generated method stub
		con = getConnection();
		String str = "无";
		try {
			//stmt = con.createStatement();
			ps = con.prepareStatement(sql);
		
			for(int i =0;i<psarr.length;i++){
				ps.setString(i+1, psarr[i]);
			}
			
			rs = ps.executeQuery();
			while(rs.next()){
			
				str = rs.getString(1);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		//closeall();
		return str;
	}


	//根据数据值向表中插入数据
	public void insertTableByArray(String sql, String[] psarr) {
		// TODO Auto-generated method stub
		con = getConnection();
		try {
			ps = con.prepareStatement(sql);
			for(int i =0;i<psarr.length;i++){
				ps.setString(i+1, psarr[i]);
			}
			ps.execute();
			System.out.println("有故障已插入到硬件故障表中");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		//closeall();
	}


	public Map<String, ArrayList<String>> getssmapfromcheckpoint(String sql) {
		// TODO Auto-generated method stub
		Map<String,ArrayList<String>> ssmap = new HashMap<String,ArrayList<String>>();
		ArrayList<String> listgps = new ArrayList<String>();
		ArrayList<String> listaddress = new ArrayList<String>();
		int i = 0;
		con = getConnection();
		try {
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			System.out.println(sql+"===");
			while(rs.next()){
				if(!StringUtil.isEmpty(rs.getString(1))){
					listgps.add(i, rs.getString(1).split(",")[1]+","+rs.getString(1).split(",")[0]);
					System.out.println(rs.getString(1).split(",")[1]+","+rs.getString(1).split(",")[0]);
					listaddress.add(i, rs.getString(2));
					i++;
				}
				
			}
			ssmap.put("listgps",listgps);
			ssmap.put("listaddress", listaddress);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		//closeall();
		
		return ssmap;
	}


	public void deleteTableRowDao(String sql1) {
		// TODO Auto-generated method stub
		con = getConnection();
		try {
			stmt =con.createStatement();
			stmt.execute(sql1);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
	}


	public Map<String, String> getGlobalVariableCacheMap() {
		// TODO Auto-generated method stub
		String sql = "select name,value from global_variable";
		Map<String,String> map = new HashMap<>();
		con = getConnection();
		try {
			stmt =con.createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				map.put(rs.getString("name"), rs.getString("value"));
				
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return map;
	}
}
